<template>
  <div>
    <h1 style="text-align: center">在这里导入excel</h1>
    <UploadExcel :on-success="handleSuccess" />
  </div>
</template>

<script>
import { formatExcelDate } from '@/utils/index'
import { importEmployee } from '@/api/employees'
export default {
  // methods:事件处理函数
  methods: {
    transExcel(results) {
      // 中英文属性名映射关系
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      // 将excel数据转换成后端的数据格式
      const res = results.map(item => {
        const zhKeys = Object.keys(item)
        const obj = {}
        zhKeys.forEach(zhKey => {
          const enKey = mapInfo[zhKey]
          if (zhKey === '入职日期' || zhKey === '转正日期') {
            obj[enKey] = new Date(formatExcelDate(item[zhKey]))
          } else {
            obj[enKey] = item[zhKey]
          }
        })
        return obj
      })
      return res
    },
    async handleSuccess({ header, results }) {
      const data = this.transExcel(results)
      const res = await importEmployee(data)
      console.log(res)
    }
  }
}
</script>

<style>

</style>
